package com.chance.cc.crawler.development.bootstrap.weibo;

import com.alibaba.fastjson.JSON;
import com.chance.cc.crawler.core.downloader.HttpConstant;
import com.chance.cc.crawler.core.downloader.HttpRequestBody;
import com.chance.cc.crawler.core.record.CrawlerRequestRecord;
import com.chance.cc.crawler.development.controller.DevCrawlerController;
import com.chance.cc.crawler.development.utils.RedisReader;
import org.apache.commons.lang3.StringUtils;

import static com.chance.cc.crawler.core.CrawlerEnum.CrawlerRequestType.*;

/**
 * @Author Zhao.Hhuan
 * @Date Create in 2021/1/25 13:45
 * @Description
 *      批量获取用户的基本信息
 **/
public class WeiboApi {

    public static final String domainId = "weibo";
    private static final String app_key = "1428199813";
    private static final String access_token = "2.00o4_w1HrAaeYBedf38e38b8SnITmD";

    private static final String usersInfoUrl = "https://c.api.weibo.com/2/users/show_batch/other.json";
    private static final String usersInfoSite = "apiUsersInfo";

    private static final String searchContainsKwPostListUrl = "https://c.api.weibo.com/2/search/statuses/limited.json";
    private static final String searchContainsKwPostListSite = "apiSearchContainsKwPostList";

    private static final String usersTagsUrl = "https://c.api.weibo.com/2/tags/tags_batch/other.json";
    private static final String usersTagsSite = "apiUsersTags";

    private static final String postInteractionUrl = "https://c.api.weibo.com/2/statuses/count/biz.json";
    private static final String postInteractionSite = "apiPostInteraction";

    private static final String postForwardsListUrl = "https://c.api.weibo.com/2/statuses/repost_timeline/all.json";
    private static final String postForwardsListSite = "apiPostForwardsList";

    private static final String userPostList = "https://c.api.weibo.com/2/statuses/user_timeline_batch.json";
    private static final String userPostListSite = "apiUserPostList";

    private static final String postLikesList = "https://c.api.weibo.com/2/attitudes/show/biz.json";
    private static final String postLikesListSite = "apiPostLikesList";

    private static final String historicalCreate = "https://c.api.weibo.com/2/search/statuses/historical/create.json";
    private static final String historicalCreateSite = "apiHistoricalCreate";

    private static final String historicalCheck = "https://c.api.weibo.com/2/search/statuses/historical/check.json";
    private static final String historicalCheckSite = "apiHistoricalCheck";

    public static void main(String[] args) {
//        searchContainsKwPostList();
        usersInfo();
//        usersTags();
//        postInteraction();
//        postForwardsList();
//        userPostList();
//        historicalCreate();
//        sycMysqlToRedis();
//        historicalCheck();
    }

    private static void usersInfo(){
        CrawlerRequestRecord requestRecord = requestRecord(usersInfoUrl, usersInfoSite);
        requestRecord.getHttpRequest().addExtra("access_token",access_token);
        requestRecord.getHttpRequest().addExtra("app_key",app_key);
        CrawlerRequestRecord keywordRecord = keywordRecord(usersInfoSite);
        controller(requestRecord,keywordRecord);
    }


    private static void searchContainsKwPostList(){
        CrawlerRequestRecord requestRecord = requestRecord(searchContainsKwPostListUrl, searchContainsKwPostListSite);
        requestRecord.getHttpRequest().addExtra("access_token",access_token);
        requestRecord.getHttpRequest().addExtra("app_key",app_key);
        CrawlerRequestRecord keywordRecord = keywordRecord("searchKw");
        controller(requestRecord,keywordRecord);
    }

    private static void usersTags(){
        CrawlerRequestRecord requestRecord = requestRecord(usersTagsUrl, usersTagsSite);
        requestRecord.getHttpRequest().addExtra("access_token",access_token);
        requestRecord.getHttpRequest().addExtra("app_key",app_key);
        CrawlerRequestRecord keywordRecord = keywordRecord(usersInfoSite);
        controller(requestRecord,keywordRecord);
    }

    private static void postInteraction(){
        CrawlerRequestRecord requestRecord = requestRecord(postInteractionUrl, postInteractionSite);
        requestRecord.getHttpRequest().addExtra("access_token",access_token);
        requestRecord.getHttpRequest().addExtra("app_key",app_key);
        CrawlerRequestRecord keywordRecord = keywordRecord(usersInfoSite);
        controller(requestRecord,keywordRecord);
    }

    private static void postForwardsList(){
        CrawlerRequestRecord requestRecord = requestRecord(postForwardsListUrl, postForwardsListSite);
        requestRecord.getHttpRequest().addExtra("access_token",access_token);
        requestRecord.getHttpRequest().addExtra("app_key",app_key);
        CrawlerRequestRecord keywordRecord = keywordRecord(usersInfoSite);
        controller(requestRecord,keywordRecord);
    }

    private static void userPostList(){
        CrawlerRequestRecord requestRecord = requestRecord(userPostList, userPostListSite);
        requestRecord.getHttpRequest().addExtra("access_token",access_token);
        requestRecord.getHttpRequest().addExtra("app_key",app_key);
        CrawlerRequestRecord keywordRecord = keywordRecord(usersInfoSite);
        controller(requestRecord,keywordRecord);
    }

    private static void postLikesList(){
        CrawlerRequestRecord requestRecord = requestRecord(postLikesList, postLikesListSite);
        requestRecord.getHttpRequest().addExtra("access_token",access_token);
        requestRecord.getHttpRequest().addExtra("app_key",app_key);
        CrawlerRequestRecord keywordRecord = keywordRecord(usersInfoSite);
        controller(requestRecord,keywordRecord);
    }

    //在上线的时候需要使用到回调函数将生成的结果放入数据中
    private static void historicalCreate(){
        CrawlerRequestRecord requestRecord = requestRecord(historicalCreate, historicalCreateSite);
        requestRecord.getHttpRequest().addExtra("access_token",access_token);
        requestRecord.getHttpRequest().addExtra("app_key",app_key);
        CrawlerRequestRecord keywordRecord = keywordRecord(historicalCreateSite);
        controller(requestRecord,keywordRecord);
    }

    private static void historicalCheck(){
        String requestQueueName = StringUtils.joinWith("-","crawler",domainId,historicalCheckSite,"queue");

        RedisReader redisReader = new RedisReader("192.168.1.215",6379,3);

        DevCrawlerController.DevCrawlerControllerBuilder devCrawlerControllerBuilder = DevCrawlerController.builder()
                .triggerInfo(domainId, domainId, System.currentTimeMillis(), domainId)
                .crawlerRequestQueue(DevCrawlerController.devRequestQueue(requestQueueName))
                .consoleResultPipeline();
        for (int i = 0; i < 1; i++) {
            CrawlerRequestRecord crawlerRequestRecord = redisReader.recordPop(requestQueueName +"_list", requestQueueName+"_map");
            devCrawlerControllerBuilder
                    .requestRecord(crawlerRequestRecord);
        }

        devCrawlerControllerBuilder.build().start();
    }


    private static CrawlerRequestRecord requestRecord(String url,String site){
        CrawlerRequestRecord requestRecord = CrawlerRequestRecord.builder()
                .startPageRequest(domainId, turnPage)
                .httpUrl(url)
                .needParsed(false)
                .releaseTime(System.currentTimeMillis())
                .build();
        requestRecord.setDownload(false);
        requestRecord.setSkipPipeline(true);
        requestRecord.tagsCreator().bizTags().addSite(site);
        return requestRecord;
    }

    private static CrawlerRequestRecord keywordRecord(String site){
        CrawlerRequestRecord keywordRecord = CrawlerRequestRecord.builder()
                .startPageRequest("weibo_search_kw",turnPageItem)
                .httpUrl("http://192.168.1.215:9599/v1/meta/"+domainId+"/keys?site=" + site)
                .requestLabelTag(supportSource)
                .requestLabelTag(internalDownload)
                .build();

        return keywordRecord;
    }

    private static void controller(CrawlerRequestRecord requestRecord,CrawlerRequestRecord keywordRecord){
        DevCrawlerController.builder()
                .triggerInfo(domainId,domainId,System.currentTimeMillis(),domainId)
                .crawlerRequestQueue(DevCrawlerController.devRequestQueue(domainId))
                .consoleResultPipeline()
                .requestRecord(requestRecord)
                .supportRecord(keywordRecord)
                .build().start();
    }

    private static void sycMysqlToRedis(){
        CrawlerRequestRecord requestRecord = CrawlerRequestRecord.builder()
                .startPageRequest(domainId, turnPageItem)
                .httpUrl(historicalCheck)
                .releaseTime(System.currentTimeMillis())
                .build();
        requestRecord.tagsCreator().bizTags().addSite(historicalCheckSite);
        requestRecord.getHttpRequest().addExtra("access_token",access_token);
        requestRecord.getHttpRequest().addExtra("app_key",app_key);

        //初始化postRecord
        String requestQueueName = StringUtils.joinWith("-","crawler",domainId,historicalCheckSite,"queue");
        CrawlerRequestRecord initCommentCrawlerRecord = CrawlerRequestRecord.builder()
                .startPageRequest("weibo_history",turnPageItem)
                .httpUrl("http://192.168.1.215:9599/crawler/domain/common/api/v1/"+domainId+"/results/queue/init?requestQueueName="+requestQueueName+"&site="+historicalCreateSite)
                .requestLabelTag(supportSource)
                .requestLabelTag(internalDownload)
                .build();
        HttpRequestBody jsonBody = HttpRequestBody.json(JSON.toJSONString(requestRecord), "utf-8");
        initCommentCrawlerRecord.getHttpRequest().setMethod(HttpConstant.Method.POST);
        initCommentCrawlerRecord.getHttpRequest().setRequestBody(jsonBody);

        DevCrawlerController.builder()
                .triggerInfo(domainId,domainId,System.currentTimeMillis(),domainId)
                .crawlerRequestQueue(DevCrawlerController.devRequestQueue(requestQueueName))
                .consoleResultPipeline()
                .requestRecord(requestRecord)
                .supportRecord(initCommentCrawlerRecord)
                .build().start();
    }
}
